import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class Example extends JPanel implements ActionListener, MouseListener{

	double x = 0.0;
	double y = 0.0;
	Timer t;
	RectangleChar[] ra;
	DragChar dc;
	Building bldg;


	public Example(JFrame f, RectangleChar[] r, DragChar d){
		ra = r;
		dc = d;
		bldg = new Building(3000, 3000);
		this.addMouseMotionListener(dc);
		this.addMouseListener(bldg);
		this.addMouseMotionListener(bldg);
		for(int i = 0; i < ra.length; i++){
			t = new Timer(10, this);
			this.setBackground(Color.BLACK);
			t.addActionListener(ra[i]);
			f.addKeyListener(ra[i]);
			t.start();
		}
		t.addActionListener(dc);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("RECT");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(1024, 720);
		frame.setResizable(true);
		RectangleChar[] rca = {
				new RectangleChar("wasd"), new RectangleChar("udlr")	
		};
		DragChar d = new DragChar();
		Example e = new Example(frame, rca, d);
		frame.add(e);
		frame.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(t)){
			this.repaint();
		}
	}

	@Override
	public void paint(Graphics g){
		Graphics2D g2d = (Graphics2D)g;
		
		super.paint(g2d);
		for(int i = 0; i < ra.length; i++){
			g2d.setColor(ra[i].getColor());
			g2d.fill(ra[i].getShape());
		}
		g2d.setColor(dc.getColor());
		g2d.fill(dc.getShape());
		g2d.setColor(new Color(0, 150, 255));
		g2d.fillRect(0, 800, 1300, 1000);
		bldg.paint(g2d);
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
	
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
}
